public class Solution8 {
    public int myAtoi(String s) {
        int cur=0;
        while (cur<s.length() && s.charAt(cur)==' '){
            cur++;
        }
        if (cur==s.length()){
            return 0;
        }
        int flag=1;
        if (s.charAt(cur)=='+'){
            cur++;
        }else if (s.charAt(cur)=='-'){
            flag=-1;
            cur++;
        }
        long ans=0;
        while (cur<s.length() && s.charAt(cur)>='0' && s.charAt(cur)<='9' &&  ans<(long)Integer.MAX_VALUE+1){
            ans=ans*10+s.charAt(cur)-'0';
            cur++;
        }
        if (flag==1){
            return ans>Integer.MAX_VALUE?Integer.MAX_VALUE:(int)ans;
        }else{
            return ans>(long)Integer.MAX_VALUE+1?Integer.MIN_VALUE:(int)(ans*flag);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution8().myAtoi("4193 with words"));
    }
}
